package jhlee42.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.FocusManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;




//로그인/로그아웃시 Jpanel을 교체
//JtextField에 placeholder 추가



public class LoginManager extends JFrame {

	private JPanel contentPane;
	private JPanel panel; // 로그인,로그아웃,배너
	private JTabbedPane tabbedPane; 

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginManager frame = new LoginManager();
					frame.setVisible(true);
					frame.requestFocusInWindow(true);
					// placeholder 가 제대로 표시되게 하기 위해
					// 현재 창에 포커스가 걸리도록 지정
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginManager() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 100));
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		
		
				
		// loginPanel 있던 자리 클래스로 뺀다..
		
		
		// 로그인 패널 추가
		//panel.add(new LoginPanel(),BorderLayout.EAST);
		panel.add(new LoginPanel(), BorderLayout.EAST);
		
		
		
		//panel.add(new LogoutPanel(), BorderLayout.WEST);
		
		
		// logoutPanel 있던 자리 클래스로 뺀다..

		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("기 본 검 색", new ImageIcon(LoginManager.class.getResource("/images/menu/new.png")), panel_1, null);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
		panel_1.add(rdbtnNewRadioButton);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("상 세 검 색", new ImageIcon(LoginManager.class.getResource("/images/menu/close.png")), panel_2, null);
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
		panel_2.add(chckbxNewCheckBox);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("마 이 페 이 지", new ImageIcon(LoginManager.class.getResource("/images/menu/exit.png")), panel_3, null);
		tabbedPane.setEnabledAt(2, false);
		// 특정 탭을 비활성화 시킴
		tabbedPane.setEnabledAt(2,false);
		
		JButton btnNewButton = new JButton("New button");
		panel_3.add(btnNewButton);
	}
	// LoginMannager 에서 선언된 변수 panel을
	// LoginPanel,LogoutPanel에서도
	// 사용할 수 있도록 getter 메서드 추가
	public JPanel getPanel() {
		return panel;
	}
	
	JTabbedPane getTab() {
		// TODO Auto-generated method stub
		return tabbedPane;
	}
	
	
} // LoginMannager










class LoginPanel extends JPanel{
	
	private JTextField uid;
	private JPasswordField pwd;
	
	
	
	public LoginPanel(){ // 기본 생성자
		
	//JPanel loginPanel = new JPanel(); // 상속 받았으므로 필요없다.
	setPreferredSize(new Dimension(220, 10));
	//panel.add(loginPanel, BorderLayout.EAST); // 상속 받았으므로 필요없다.
	
	
	
	// placeholder 기능을 추가하기 위해
	// JTextField 기본 생성자를 변경함
	uid = new JTextField(){
		@Override
		protected void paintComponent(Graphics g) {
			//Jcomponent에 미리 정의된 대로
			//JTextFiled 를 표시함
			super.paintComponent(g);
			//placeholder를 표시하기 위해
			//먼저, Graphic2D 객체 생성
			if (getText().isEmpty() /*&& FocusManager.getCurrentKeyboardFocusManager().getFocusOwner()!=this*/) {
				Graphics2D g2 = (Graphics2D)g.create();
				g2.setColor(new Color(0,0,255,55)); // 글자색
				g2.drawString("아이디", 5, 14);
				g2.dispose();
			}
		}
	};
	
	
	
	
	uid.setColumns(10);
	
	pwd = new JPasswordField(){
		@Override
		protected void paintComponent(Graphics g) {
			//Jcomponent에 미리 정의된 대로
			//JTextFiled 를 표시함
			super.paintComponent(g);
			//placeholder를 표시하기 위해
			//먼저, Graphic2D 객체 생성
			if (getText().isEmpty() /*&& FocusManager.getCurrentKeyboardFocusManager().getFocusOwner()!=this*/) {
				Graphics2D g2 = (Graphics2D)g.create();
				g2.setColor(new Color(0,0,255,55)); // 글자색
				g2.drawString("비밀번호", 5, 14);
				g2.dispose();
			}
			
		}

		
	};
	pwd.setColumns(10);
	
	
	JButton logInBtn = new JButton("로그인");
	logInBtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// 로그인 처리
			if (uid.getText().equals("123") && pwd.getText().equals("123")) {
				// 패널을 바꾸기 위해 부모 프레임을 알아봄
				LoginManager parent = (LoginManager)SwingUtilities.windowForComponent((Component)e.getSource());
				parent.getPanel().add(new LogoutPanel(),BorderLayout.EAST);
				//로그인 패널은 숨김
				setVisible(false);		
				// 특정 탭을 활성화 시킴
				parent.getTab().setEnabledAt(2,true);
			}
			else{
				JOptionPane.showMessageDialog(null, "로그인 실패! 아이디/비밀번호를 확인하세요!");
				// 입력한 내용 지우기
				uid.setText("");
				pwd.setText("");
			}
			
			
			
			
			
			
			
		}


	});
	logInBtn.setFont(new Font("굴림", Font.BOLD, 11));
	logInBtn.setMaximumSize(new Dimension(81, 23));
	logInBtn.setPreferredSize(new Dimension(81, 23));
	
	JButton joinBtn = new JButton("회원 가입");
	joinBtn.setFont(new Font("굴림", Font.BOLD, 11));
	GroupLayout gl_loginPanel = new GroupLayout(this); // (loginPanel)
	gl_loginPanel.setHorizontalGroup(
		gl_loginPanel.createParallelGroup(Alignment.LEADING)
			.addGroup(gl_loginPanel.createSequentialGroup()
				.addContainerGap()
				.addGroup(gl_loginPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_loginPanel.createSequentialGroup()
						.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(logInBtn))
					.addGroup(gl_loginPanel.createSequentialGroup()
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(joinBtn)))
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	);
	gl_loginPanel.setVerticalGroup(
		gl_loginPanel.createParallelGroup(Alignment.LEADING)
			.addGroup(gl_loginPanel.createSequentialGroup()
				.addGap(20)
				.addGroup(gl_loginPanel.createParallelGroup(Alignment.BASELINE)
					.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(logInBtn))
				.addGap(18)
				.addGroup(gl_loginPanel.createParallelGroup(Alignment.BASELINE)
					.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(joinBtn))
				.addContainerGap(20, Short.MAX_VALUE))
	);
//	loginPanel.setLayout(gl_loginPanel);
	setLayout(gl_loginPanel);
	
	
	
	
	} // 생성자
	
} // class LoginPanel
	
	
	
	
	
class LogoutPanel extends JPanel{
	
	
	
	public LogoutPanel(){ // 기본 생성자
		
	

//		JPanel logoutPanel = new JPanel();
//		logoutPanel.setPreferredSize(new Dimension(225, 10));
//		panel.add(logoutPanel, BorderLayout.WEST);

	setPreferredSize(new Dimension(225, 10));

	
	
	JButton logoutBtn = new JButton("로그아웃");
	logoutBtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// 로그 아웃 처리
	
			
			LoginManager parent = (LoginManager)SwingUtilities.windowForComponent((Component)e.getSource());
			parent.getPanel().add(new LogoutPanel(),BorderLayout.EAST);
			//로그인 패널 추가
			parent.getPanel().add(new LoginPanel(),BorderLayout.EAST);
			//로그인 패널 숨김
			setVisible(false);
			// 특정 탭을 활성화 시킴
			parent.getTab().setEnabledAt(2,false);
		}
	});
	
	

	JLabel lblNewLabel = new JLabel("New label");
	lblNewLabel.setIcon(new ImageIcon(LoginManager.class.getResource("/images/background/about.png")));
	GroupLayout gl_logoutPanel = new GroupLayout(this); // logoutPanel
	gl_logoutPanel.setHorizontalGroup(
		gl_logoutPanel.createParallelGroup(Alignment.LEADING)
			.addGroup(Alignment.TRAILING, gl_logoutPanel.createSequentialGroup()
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(ComponentPlacement.UNRELATED)
				.addComponent(logoutBtn)
				.addGap(22))
	);
	gl_logoutPanel.setVerticalGroup(
		gl_logoutPanel.createParallelGroup(Alignment.LEADING)
			.addGroup(gl_logoutPanel.createSequentialGroup()
				.addGap(22)
				.addGroup(gl_logoutPanel.createParallelGroup(Alignment.LEADING)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
					.addComponent(logoutBtn))
				.addContainerGap(16, Short.MAX_VALUE))
	);
//	logoutPanel.setLayout(gl_logoutPanel);
	setLayout(gl_logoutPanel);
	} // 생성자
	
} // class LogoutPanel























